package Z.dailyExercise.May;

import java.util.Arrays;

public class _11盛最多水的容器 {
    public static void main(String[] args) {
        _11盛最多水的容器 ts = new _11盛最多水的容器();
        ts.maxArea(new int[]{1,8,6,2,5,4,8,3,7});
    }

    /**
     * 对每一个gap求一个最大数值 然后再比较最大
     *超时了
     * @param height
     * @return
     */
    public int maxArea(int[] height) {

        int n = height.length;
        int[] res = new int[n-1];
        int index = 0;
        for (int gap=1;gap < n;gap++){
            int size = Integer.MIN_VALUE;
            for (int i = 0; i <= n-gap; i++) {
                size = Math.max(size,gap * Math.min(height[i+gap],height[i]));

            }
            res[index++] = size;
        }

        Arrays.sort(res);
        return res[n-1];


    }

    /**
     * 双指针
     */
    public int maxArea1(int[] height) {
        int i = 0;
        int j = height.length-1;
        int size = Integer.MIN_VALUE;

        while (i!= j){

            size = Math.max(size,(j-i) *  Math.min(height[i],height[j]));
            if (height[i]<height[j]){
                i++;
            }else {
                j--;
            }
        }

        return size;
    }
}
